package com.nakaikensuke.tools;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class ItemAdapter<T> extends ArrayAdapter<T> {
	private List<T> _list = null;
	private LayoutInflater _inflater = null;
	private int _layoutId = 0;
	private ItemComposer<T> _composer = null;
	
	public ItemAdapter(
				Context context,
				int id,
				List<T> list,
				ItemComposer<T> composer) {
		super(context, id, list);
		
		_composer = composer;
		_layoutId = id;
		_list = list;
		_inflater = (LayoutInflater)context.getSystemService(
													Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		
		if(view == null)
			view = _inflater.inflate(_layoutId, null);
		
		if(position >= _list.size())
			return view;
		
		_composer.compose(view, _list.get(position), position);
		
		return view;
	}
}
